package com.gking.processHarvest.service.ShareProject;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.processHarvest.common.BaseContext;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.dto.GetFriendShareProject.GetFriendShareProjectDto;
import com.gking.processHarvest.dto.GetFriendShareProject.ShareProjectExampleDto;
import com.gking.processHarvest.dto.GetFriendShareProject.ShareProjectHarvestDto;
import com.gking.processHarvest.dto.ShareProjectDetailDto;
import com.gking.processHarvest.entity.*;
import com.gking.processHarvest.mapper.ShareProjectMapper;
import com.gking.processHarvest.service.Example.ExampleService;
import com.gking.processHarvest.service.Friends.FriendsService;
import com.gking.processHarvest.service.Harvest.HarvestService;
import com.gking.processHarvest.service.Notes.NotesService;
import com.gking.processHarvest.service.Project.ProjectService;
import com.gking.processHarvest.service.ShareProject.ShareProjectService;
import com.gking.processHarvest.utils.CheckStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class ShareProjectServiceImpl extends ServiceImpl<ShareProjectMapper, ShareProject> implements ShareProjectService {

    @Autowired
    private FriendsService friendsService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ExampleService exampleService;

    @Autowired
    private NotesService notesService;

    @Autowired
    private HarvestService harvestService;


    /**
     * 获取分享给好友的项目详情列表
     *
     * @param friendId 好友id
     * @return s
     */
    @Override
    public Res<List<ShareProjectDetailDto>> getShareFriendList(String friendId) {

        //查询所有的项目
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Project::getMasterId, BaseContext.getCurrentId());
        queryWrapper.eq(Project::getIsDelete, 0);
        queryWrapper.orderByAsc(Project::getCreateTime);

        List<ShareProjectDetailDto> list = projectService.list(queryWrapper).stream().map(item -> {

            //对每一个项目进行分析比对，查看分享情况
            LambdaQueryWrapper<ShareProject> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShareProject::getMasterId, BaseContext.getCurrentId());
            wrapper.eq(ShareProject::getFriendId, friendId);
            wrapper.eq(ShareProject::getProjectId, item.getId());

            ShareProject one = this.getOne(wrapper);

            ShareProjectDetailDto shareProjectDetailDto = new ShareProjectDetailDto();
            shareProjectDetailDto.setProjectId(item.getId());
            shareProjectDetailDto.setTitle(item.getTitle());
            shareProjectDetailDto.setDescription(item.getDescription());
            shareProjectDetailDto.setCreateTime(item.getCreateTime());

            if (one == null) shareProjectDetailDto.setIsShare(false);
            else shareProjectDetailDto.setIsShare(true);

            return shareProjectDetailDto;

        }).collect(Collectors.toList());

        return Res.success("查询成功！", list);
    }

    /**
     * 分享项目
     *
     * @param friendId  好友用户id
     * @param projectId 项目id
     * @return s
     */
    @Override
    public Res<String> add(String friendId, String projectId) {
        if (friendId == null || projectId == null) return Res.error("请将参数补充完整！");

        //是否为好友
        LambdaQueryWrapper<Friends> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Friends::getFriendId, friendId);
        lambdaQueryWrapper.eq(Friends::getMasterId, BaseContext.getCurrentId());
        if (friendsService.getOne(lambdaQueryWrapper) == null) return Res.error("此用户不是好友，无法分享项目！");

        //项目是否存在且未删除
        CheckStatus<Project> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Project> queryWrapper = checkStatus.checkIsDelete(
                projectId, Project::getId,
                Project::getMasterId, Project::getIsDelete);
        Project project = projectService.getOne(queryWrapper);
        if (project == null) return Res.error("项目已删除，无法分享！");

        //项目是否已经分享了
        LambdaQueryWrapper<ShareProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShareProject::getFriendId, friendId);
        wrapper.eq(ShareProject::getProjectId, projectId);
        wrapper.eq(ShareProject::getMasterId, BaseContext.getCurrentId());
        if (this.getOne(wrapper) != null) return Res.error("项目已经分享了，无法重复分享");

        //保存数据
        ShareProject shareProject = new ShareProject();
        shareProject.setProjectId(projectId);
        shareProject.setProjectTitle(project.getTitle());
        shareProject.setFriendId(friendId);
        this.save(shareProject);

        return Res.success("分享项目成功！");
    }


    /**
     * 删除项目
     *
     * @param friendId  好友用户id
     * @param projectId 项目id
     * @return s
     */
    @Override
    public Res<String> delete(String friendId, String projectId) {
        if (friendId == null || projectId == null) return Res.error("请将参数补充完整！");

        //查询数据
        LambdaQueryWrapper<ShareProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShareProject::getProjectId, projectId);
        queryWrapper.eq(ShareProject::getFriendId, friendId);
        queryWrapper.eq(ShareProject::getMasterId, BaseContext.getCurrentId());

        if (this.getOne(queryWrapper) == null) return Res.error("删除失败！");

        this.remove(queryWrapper);

        return Res.success("删除成功！");
    }


    /**
     * 获取好友分享给自己的项目
     *
     * @param friendId 好友id
     * @return s
     */
    @Override
    public Res<List<ShareProject>> getFriendShareList(String friendId) {

        LambdaQueryWrapper<ShareProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShareProject::getMasterId, friendId);
        queryWrapper.eq(ShareProject::getFriendId, BaseContext.getCurrentId());

        List<ShareProject> list = this.list(queryWrapper);
        return Res.success("获取好友分享给自己的项目成功！", list);
    }


    /**
     * 获取好友分享给自己的项目详情
     *
     * @param projectId 项目id
     * @return s
     */
    @Override
    public Res<GetFriendShareProjectDto> getFriendProjectDetail(String projectId) {

        //先查询好友有没有分享此项目给用户
        LambdaQueryWrapper<ShareProject> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ShareProject::getProjectId, projectId);
        queryWrapper1.eq(ShareProject::getFriendId, BaseContext.getCurrentId());
        ShareProject shareProject = this.getOne(queryWrapper1);
        if (shareProject == null) return Res.error("好友分享的项目中没有此项目！");

        //获取分享的项目信息
        LambdaQueryWrapper<Project> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Project::getId, projectId);
        queryWrapper2.eq(Project::getMasterId, shareProject.getMasterId());
        Project project = projectService.getOne(queryWrapper2);
        if (project == null) return Res.error("获取项目详情信息失败");

        //获取项目中的收获列表
        LambdaQueryWrapper<Harvest> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(Harvest::getProjectId, projectId);
        queryWrapper3.eq(Harvest::getMasterId, shareProject.getMasterId());
        List<Harvest> harvestList = harvestService.list(queryWrapper3);

        //获取每一个收获的示例列表
        List<ShareProjectHarvestDto> shareProjectHarvestDtoList = harvestList.stream().map(harvestItem -> {
            //获取收获的示例列表
            LambdaQueryWrapper<Example> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(Example::getProjectId, projectId);
            queryWrapper4.eq(Example::getMasterId, shareProject.getMasterId());
            queryWrapper4.eq(Example::getHarvestId, harvestItem.getId());
            List<Example> exampleList = exampleService.list(queryWrapper4);

            //获取示例列表后，获取示例的笔记列表
            List<ShareProjectExampleDto> shareProjectExampleDtoList = exampleList.stream().map(exampleItem -> {
                //获取示例下的所有笔记列表
                LambdaQueryWrapper<Notes> queryWrapper5 = new LambdaQueryWrapper<>();
                queryWrapper5.eq(Notes::getProjectId, projectId);
                queryWrapper5.eq(Notes::getMasterId, shareProject.getMasterId());
                queryWrapper5.eq(Notes::getExampleId, exampleItem.getId());
                List<Notes> notesList = notesService.list(queryWrapper5);

                //整合示例详情，以及示例的所有笔记列表详情
                ShareProjectExampleDto shareProjectExampleDto = new ShareProjectExampleDto();
                shareProjectExampleDto.setExample(exampleItem);
                shareProjectExampleDto.setNotesList(notesList);

                return shareProjectExampleDto;
            }).collect(Collectors.toList());

            //整合收获详情，以及收获的所有示例列表详情
            ShareProjectHarvestDto shareProjectHarvestDto = new ShareProjectHarvestDto();
            shareProjectHarvestDto.setHarvest(harvestItem);
            shareProjectHarvestDto.setShareProjectExampleDtoList(shareProjectExampleDtoList);

            return shareProjectHarvestDto;
        }).collect(Collectors.toList());

        //整合项目详情，以及项目的所有收获列表详情
        GetFriendShareProjectDto getFriendShareProjectDto = new GetFriendShareProjectDto();
        getFriendShareProjectDto.setProject(project);
        getFriendShareProjectDto.setShareProjectHarvestDtoList(shareProjectHarvestDtoList);

        return Res.success("获取好友分享的项目详情成功！", getFriendShareProjectDto);
    }
}
